package com.chebaohui.iqs.service.impl.deal.taiping.profess;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONArray;
import com.chebaohui.iqs.config.CompanyCode;
import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.base.Cars;
import com.chebaohui.iqs.entity.req.CheckCar;
import com.chebaohui.iqs.entity.res.CompanyCars;
import com.chebaohui.iqs.service.impl.deal.taiping.ResTaiPing;
import com.chebaohui.iqs.service.impl.deal.taiping.ev.E05Request;
import com.chebaohui.iqs.service.impl.deal.util.StringUtil;

/**
 * 太平车辆查询类
 * 
 * @author regas_xie
 *
 */
public class TpCheckCar {

	/**
	 * log:日志输出
	 */
	private static Log log = LogFactory.getLog(TpCheckCar.class);

	/**
	 * 太平查车
	 * 
	 * @param carParam
	 *            参数
	 * @return
	 */
	public static CompanyCars<List<Cars>> tpCheckCar(CheckCar carParam) {
		log.info("进入" + CompanyValue.TAIPING + "查询车型"+carParam.getLicense().getLableNo()+"....");
		try {
			ResTaiPing res = E05Request.requestE05("", carParam.getLicense().getLableNo());
			log.info("查询车型"+carParam.getLicense().getLableNo()+",log记录:" + res.getMsg());
			if (res.isState()) {
				JSONArray list = res.getResult().getJSONArray("vehicle");
				if (null == list || list.isEmpty() || list.size() < 1) {
					return new CompanyCars<List<Cars>>(CompanyCode.TAIPING.getCode(), ResultCode.N04.getCode(),
							ResultCode.N04.getMsg() + "无此车型数据");
				} else {
					CompanyCars<List<Cars>> result = new CompanyCars<List<Cars>>(CompanyCode.TAIPING.getCode(),
							ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
					log.info(CompanyValue.TAIPING + ",查询车型"+carParam.getLicense().getLableNo()+",处理返回结果..");
					result.setCars(dealCars(list));
					return result;
				}
			} else {
				return new CompanyCars<List<Cars>>(CompanyCode.TAIPING.getCode(), res.getError().getCode(),
						res.getError().getMsg() + (StringUtil.isNulls(res.getErrMsg()) ? "" : res.getErrMsg()));
			}
		} catch (Exception e) {
			log.error(CompanyValue.TAIPING + "查询车型"+carParam.getLicense().getLableNo()+"过程过程异常:" + e.getMessage());
			return new CompanyCars<List<Cars>>(CompanyCode.TAIPING.getCode(), ResultCode.N02.getCode(),
					ResultCode.N02.getMsg() + "查询车型"+carParam.getLicense().getLableNo()+"过程过程异常:" + e.getMessage());
		} finally {
			log.info(CompanyValue.TAIPING + "查询车型"+carParam.getLicense().getLableNo()+"结束。");
		}
	}

	/**
	 * 处理接口返回结果
	 * 
	 * @param list
	 *            车型列表
	 * @return
	 */
	private static List<Cars> dealCars(JSONArray list) {
		List<Cars> res = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			Cars c = new Cars();
			c.setModelCode(list.getJSONObject(i).getString("modelCode"));
			c.setModelName(list.getJSONObject(i).getString("carBrand"));
			c.setExhausMeasur(list.getJSONObject(i).getString("exhaustScale"));
			c.setPurchase(list.getJSONObject(i).getString("purchasePrice"));
			c.setRemark(list.getJSONObject(i).getString("farmilyName")+" "+list.getJSONObject(i).getString("manualFlag"));
			String[] cm = list.getJSONObject(i).getString("configModel").split(" ");
			if (cm.length > 1)
				c.setRemark(cm[0] + cm[1]);
			for (String r : cm) {
				if (r.indexOf("款") > 0) {
					String year = r.substring(0, r.indexOf("款"));
					if (StringUtil.valFormat(year, "^[0-9/-]{4,}$"))
						c.setSaleDate(year);

					continue;
				} else if (r.indexOf("座") > 0) {
					String seat = r.substring(0, r.indexOf("座"));
					if (StringUtil.valFormat(seat, "^[0-9]{1,2}$"))
						c.setSeats(Integer.valueOf(seat));

					continue;
				}
			}

			c.setOption(c.getRemark()+CompanyValue.SEG
					+c.getModelCode()+CompanyValue.SEG
					+c.getModelName()+CompanyValue.SEG
					+c.getExhausMeasur()+CompanyValue.SEG
					+c.getSaleDate()+CompanyValue.SEG
					+c.getPurchase()+CompanyValue.SEG
					+c.getSeats()
					);
			
			res.add(c);
		}

		return res;
	}
}
